package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by root on 4/24/14.
 */
public class Library {
    private List<Book> availableBooks = new ArrayList<Book>();
    private List<Book> checkedOutBooks = new ArrayList<Book>();
    private List<Customer> customers = new ArrayList<Customer>();
    private List<Movie> movies = new ArrayList<Movie>();

    private static boolean hasObject = false;
    private static Library library = null;

    public static Library getObject() {
        if (hasObject) {
            return library;
        } else {
            hasObject = true;
            library = new Library();
            return library;
        }
    }


    public List<Book> getCheckedOutBooks() {
        return checkedOutBooks;
    }

    public void setAvailableBooks(Book availableBook) {
        this.availableBooks.add(availableBook);
    }

    public void setCheckedOutBooks(Book checkedOutBook) {
        this.checkedOutBooks.add(checkedOutBook);
    }

    public void setCustomer(Customer customer) {
        this.customers.add(customer);
    }

    public List<Book> getAvailableBooks() {
        return availableBooks;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setMovies(Movie movie) {
        this.movies.add(movie);
    }

    public List<Movie> getMovies() {
        return movies;
    }

    public Customer getCustomer(String customerId){
        Customer requiredCustomer = new Customer("","","","","");
        for(Customer customer:customers){
            if(customer.getLibraryNumber().equals(customerId))
                requiredCustomer = customer;
        }

        return requiredCustomer;
    }

    public void removeFromCheckedOutBooks(Book book){

        int index = this.checkedOutBooks.indexOf(book);
        this.checkedOutBooks.remove(index);

    }

    public void removeFromAvailableBooks(Book book){

        int index = this.availableBooks.indexOf(book);
        this.availableBooks.remove(index);

    }

    public void returnBook(String option){

        BibliotecaApp app = new BibliotecaApp();
        Book book = new Book("","","","","");
        String message = "Book has been returned successfully";
        boolean valid = false ;
        app.showCheckedOutBooks();
        if(option.equals(""))
            option = app.getCustomerInput();
        for(Book bookInstance : library.getCheckedOutBooks()) {
            if (bookInstance.getId().equals(option)) {
                book = bookInstance;
                valid = true;
            }
        }

        if(valid){
            Customer customer = library.getCustomer("AAA-1234");
            customer.removeCheckedOutBook(book);
            book.setAvailable(true);
            library.removeFromCheckedOutBooks(book);
            library.setAvailableBooks(book);
        }


        app.printValidity(valid, message);

    }

    public void checkoutMovie(String option, List<Movie> movies){

        boolean checked = getCustomer("AAA-1234").checkIfCheckedOut(option);
        BibliotecaApp app =  new BibliotecaApp();

        if(checked){
            app.result="The movie has been checked out already";
            System.out.println(app.result);
            app.showMenu("");
        }
        else {
            boolean valid = false;
            Movie movie = new Movie("", 0, "", 0, "");
            String message = "Movie has been checked out successfully";
            for (Movie movieInstance : movies) {
                if (movieInstance.getId().equals(option)) {
                    movie = movieInstance;
                    valid = true;
                }
            }
            if (valid) {
                Customer customer =library.getCustomer("AAA-1234");
                customer.setCheckedOutMovie(movie);

            }
            app.printValidity(valid, message);
        }

    }

    public void checkoutBook(String option, List<Book> availableBooks){

        BibliotecaApp app =  new BibliotecaApp();
        boolean valid = false ;
        Book book = new Book("","","","","");
        String message= "Book has been checked out successfully";
        for(Book bookInstance : availableBooks){
            if(bookInstance.getId().equals(option)){
                book = bookInstance;
                valid = true;
            }
        }
        if(valid){
            Customer customer = library.getCustomer("AAA-1234");
            customer.setCheckedOutBook(book);
            book.setAvailable(false);
            book.setCheckedoutCustomer(customer);
            library.removeFromAvailableBooks(book);
            library.setCheckedOutBooks(book);
        }
        app.printValidity(valid,message);
    }

}
